 package rejahealth.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.HashMap; 
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import rejahealth.model.Person;
import rejahealth.util.*;


public final class MainFrame extends JFrame {
    
    public Person person;
    public EnergySensor sensor;
    public int climate;
     
    public MainFrame(Person _person, EnergySensor sensor_) {
       initComponents();
       //centerWindow(this);
       setExtendedState(MAXIMIZED_BOTH);
       //setSize(800,600);//padrão IESB
       setframeicon(this, "../assets/title-icon.png");
       
       person = _person;
       sensor = sensor_;
       refreshWeather(person, sensor);       
       initRefresh();
    }
    
    public void setframeicon(JFrame window, String icon){ //mudar ícone da janela
        try{ 
            InputStream imgStream = this.getClass().getResourceAsStream(icon); 
            BufferedImage bi = ImageIO.read(imgStream); 
            ImageIcon myImg = new ImageIcon(bi); 
            this.setIconImage(myImg.getImage()); 
        } catch(Exception e){ 
            System.out.println(e); 
        }    
    } 
    
    public static void centerWindow(Window frame) { //centralizar a janela
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }
    
    public void initRefresh() { //atualizar tela a cada 10 segundos
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    SwingUtilities.invokeLater(new Runnable() {
                         public void run() {
                           refreshWeather(person, sensor);
                         }
                    });
                    try { Thread.sleep(10000); } catch(Exception e) {}
                }
            }
        }).start();
    }
    
    protected void refreshWeather(Person person, EnergySensor sensor) {
        //comentar as duas linhas abaixo para ignorar o modo aleatório
        //RandomPersonTemp randomPerson = new RandomPersonTemp();
        //person = randomPerson.getTempPerson();
        person = sensor.getDados();
        Map<String, Object> result = Recommendations.recommendationGeneral(person);
        climate = Integer.parseInt(result.get("climate")+"");
        
        //comentar as duas linhas abaixo para ignorar os dados captados pelo arduino
        //EnergySensor sensor = new EnergySensor();
        //person = sensor.getDados();
        
        lblTempAtual.setText(person.getTemperature() + "ºC");
        lblUmidAtual.setText(person.getHumidity() + "%");
        
        if (person.getMeasure() == 1)
        {
            RecommendationFrame recommendationFrame = new RecommendationFrame(person, climate);
            recommendationFrame.setVisible(true);
        }
        if ( (person.getTemperature() >= 20) && (person.getTemperature() < 25.9)){
            imgTempSad.setVisible(false);
            imgTempSmile.setVisible(true);
        } else{
            imgTempSmile.setVisible(false);
            imgTempSad.setVisible(true);
        }
        if ( (person.getHumidity()>= 30) && (person.getHumidity() < 70)){
            imgUmidSad.setVisible(false);
            imgUmidSmile.setVisible(true);
        } else{
            imgUmidSmile.setVisible(false);
            imgUmidSad.setVisible(true);
        }
        txRecomendacao1.setText(result.get("recommendation").toString());
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        lblTitulo = new javax.swing.JPanel();
        lblTempAtual = new javax.swing.JLabel();
        imgTempSmile = new javax.swing.JLabel();
        imgTempSad = new javax.swing.JLabel();
        lblUmidAtual = new javax.swing.JLabel();
        lblRecomendacao = new javax.swing.JLabel();
        digitalClock1 = new rejahealth.util.DigitalClock();
        jLabel1 = new javax.swing.JLabel();
        lblLocal = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        imgUmidSmile = new javax.swing.JLabel();
        imgUmidSad = new javax.swing.JLabel();
        txRecomendacao1 = new javax.swing.JLabel();
        background1 = new javax.swing.JLabel();

        jFormattedTextField1.setText("jFormattedTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        getContentPane().setLayout(null);

        lblTitulo.setBackground(new java.awt.Color(204, 204, 204));
        lblTitulo.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblTitulo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        javax.swing.GroupLayout lblTituloLayout = new javax.swing.GroupLayout(lblTitulo);
        lblTitulo.setLayout(lblTituloLayout);
        lblTituloLayout.setHorizontalGroup(
            lblTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 70, Short.MAX_VALUE)
        );
        lblTituloLayout.setVerticalGroup(
            lblTituloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        getContentPane().add(lblTitulo);
        lblTitulo.setBounds(1380, 0, 70, 50);

        lblTempAtual.setFont(new java.awt.Font("Tahoma", 0, 62)); // NOI18N
        lblTempAtual.setText("Temp");
        getContentPane().add(lblTempAtual);
        lblTempAtual.setBounds(160, 320, 220, 90);

        imgTempSmile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/green-smile.PNG"))); // NOI18N
        getContentPane().add(imgTempSmile);
        imgTempSmile.setBounds(180, 150, 190, 150);

        imgTempSad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/red-sad.PNG"))); // NOI18N
        getContentPane().add(imgTempSad);
        imgTempSad.setBounds(190, 150, 150, 150);

        lblUmidAtual.setFont(new java.awt.Font("Tahoma", 0, 62)); // NOI18N
        lblUmidAtual.setText("Umid");
        getContentPane().add(lblUmidAtual);
        lblUmidAtual.setBounds(950, 330, 230, 80);

        lblRecomendacao.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblRecomendacao.setText("Recomendações:");
        getContentPane().add(lblRecomendacao);
        lblRecomendacao.setBounds(550, 130, 203, 29);

        digitalClock1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        getContentPane().add(digitalClock1);
        digitalClock1.setBounds(310, 670, 460, 50);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/graus.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 240, 770);

        lblLocal.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        lblLocal.setText("Brasília - DF ");
        getContentPane().add(lblLocal);
        lblLocal.setBounds(780, 670, 220, 50);

        jButton1.setText("Next");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(970, 550, 100, 70);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/umidade.png"))); // NOI18N
        getContentPane().add(jLabel3);
        jLabel3.setBounds(1100, 0, 240, 770);

        imgUmidSmile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/green-smile.PNG"))); // NOI18N
        getContentPane().add(imgUmidSmile);
        imgUmidSmile.setBounds(970, 160, 170, 150);

        imgUmidSad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/red-sad.PNG"))); // NOI18N
        getContentPane().add(imgUmidSad);
        imgUmidSad.setBounds(990, 150, 160, 160);

        txRecomendacao1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        getContentPane().add(txRecomendacao1);
        txRecomendacao1.setBounds(450, 170, 420, 370);

        background1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rejahealth/assets/cloud.jpeg"))); // NOI18N
        getContentPane().add(background1);
        background1.setBounds(0, 0, 1370, 770);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        RecommendationFrame recommendationFrame = new RecommendationFrame(person, climate);
        recommendationFrame.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new MainFrame().setVisible(true);
//            }
//        });
    }    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel background1;
    private rejahealth.util.DigitalClock digitalClock1;
    private javax.swing.JLabel imgTempSad;
    private javax.swing.JLabel imgTempSmile;
    private javax.swing.JLabel imgUmidSad;
    private javax.swing.JLabel imgUmidSmile;
    private javax.swing.JButton jButton1;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblLocal;
    private javax.swing.JLabel lblRecomendacao;
    private javax.swing.JLabel lblTempAtual;
    private javax.swing.JPanel lblTitulo;
    private javax.swing.JLabel lblUmidAtual;
    private javax.swing.JLabel txRecomendacao1;
    // End of variables declaration//GEN-END:variables


}
